package action;

//import java.util.ArrayList;

import java.util.ArrayList;

import models.DeveloperData;
import com.opensymphony.xwork2.ActionSupport;


public class DeveloperRegistrationAction extends ActionSupport {
	
	private static final long serialVersionUID = 1L;
	private String developername;
	private String uname;
	private String password;
	private String password2;
	private String dev_design;
	private String project_id;
	private int proj_id;
	
	public String getPassword2() {
		return password2;
	}
	public void setPassword2(String password2) {
		this.password2 = password2;
	}
	private String email;
	public String getDevelopername() {
		return developername;
	}
	public void setDevelopername(String developername) {
		this.developername = developername;
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getDev_design() {
		return dev_design;
	}
	public void setDev_design(String dev_design) {
		this.dev_design = dev_design;
	}
	
	public ArrayList<DeveloperData> getDevelopers() {
		return Developers;
	}
	public void setDevelopers(ArrayList<DeveloperData> developers) {
		Developers = developers;
	}
	public int getProj_id() {
		return proj_id;
	}
	public void setProj_id(int proj_id) {
		this.proj_id = proj_id;
	}

    public String getProject_id() {
		return project_id;
	}
	public void setProject_id(String project_id) {
		this.project_id = project_id;
	}
	private ArrayList<DeveloperData> Developers = new ArrayList<DeveloperData>();
	public String execute() {
		if(this.email.length()==0)
		{
			addActionError(getText("email id cannot be empty"));
			return "error" ;
		}
		  //Assigning the email format regular expression
		  String emailPattern="^([A-Za-z0-9_\\-\\.])+\\@([A-Za-z0-9_\\-\\.])+\\.([A-Za-z]{2,4})";
		  if(false==this.email.matches(emailPattern))
		  {
			  addActionError(getText("email format wrong"));
				return "error" ;
		  }
		System.out.println("--------------------"+this.project_id+"------------------");
		if (this.developername.isEmpty()||this.uname.isEmpty()||this.password.isEmpty()
				||this.email.isEmpty()||this.dev_design.isEmpty()||this.project_id.isEmpty()) {
			addActionError(getText("No field should be empty !! Please fill the full details... !! "));
			return "error";
			}
			else if(! this.password.equals(password2)){
				addActionError(getText("password re-entered does not match !! please try again... !! "));
				return "error";
			}
		Developers=DeveloperData.find("");
		for(int i=0;i<Developers.size();i++){
			if(Developers.get(i).getUname().equals(this.uname)){
				addActionError(getText("The Username already exists.. Try again !! "));
				return "error" ;
				
			}
			else if(Developers.get(i).getEmail().equals(this.email)){
				addActionError(getText("Another Account is already registered with this Email.. Try with another Email!! "));
				return "error" ;
				
			}
		}
		this.setProj_id(Integer.parseInt(this.getProject_id()));
		DeveloperData Developer = new DeveloperData();
		Developer.setDevelopername(this.developername);
		Developer.setUname(this.uname);
		Developer.setPassword(this.password);
		Developer.setEmail(this.email);
		Developer.setDev_design(this.dev_design);
		Developer.setProject_id(this.proj_id);
		Developer.insert();
		Developers = DeveloperData.find("");
		addActionError(getText("You have been Registered as a developer please login..."));
		return "success";
	}
	
}
